home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / include / ctype.h < prev    next >
C/C++ Source or Header  |  1990-07-23  |  3KB  |  70 lines

  1. /* The <ctype.h> header file defines some macros used to identify characters.
  2.  * It works by using a table stored in ctype.c.  When a character is presented
  3.  * to one of these macros, the character is used as an index into the table
  4.  * (__ctype) to retrieve a byte.  The relevant bit is then extracted.
  5.  */
  6.  
  7. #ifndef _CTYPE_H
  8. #define _CTYPE_H
  9.  
  10. extern unsigned char __ctype[];    /* property array declared in ctype.c */
  11. extern unsigned char __tmp;    /* scratch variable declared in ctype.c */
  12.  
  13. #define _U        0001    /* this bit is for upper-case letters [A-Z] */
  14. #define _L        0002    /* this bit is for lower-case letters [a-z] */
  15. #define _N        0004    /* this bit is for numbers [0-9] */
  16. #define _S        0010    /* this bit is for white space \t \n \f etc */
  17. #define _P        0020    /* this bit is for punctuation characters */
  18. #define _C        0040    /* this bit is for control characters */
  19. #define _X        0100    /* this bit is for hex digits [a-f] and [A-F]*/
  20. #define _SP        0200    /* this bit is for the space character only */
  21.  
  22. /* Function Prototypes (have to go before the macros). */
  23. #ifndef _ANSI_H
  24. #include <ansi.h>
  25. #endif
  26.  
  27. _PROTOTYPE( int isalnum, (int  __c)  );    /* alphanumeric [a-z], [A-Z], [0-9] */
  28. _PROTOTYPE( int isalpha, (int  __c)  );    /* alphabetic */
  29. _PROTOTYPE( int iscntrl, (int  __c)  );    /* control characters */
  30. _PROTOTYPE( int isdigit, (int  __c)  );    /* digit [0-9] */
  31. _PROTOTYPE( int isgraph, (int  __c)  );    /* graphic character */
  32. _PROTOTYPE( int islower, (int  __c)  );    /* lower-case letter [a-z] */
  33. _PROTOTYPE( int isprint, (int  __c)  );    /* printable character */
  34. _PROTOTYPE( int ispunct, (int  __c)  );    /* punctuation mark */
  35. _PROTOTYPE( int isspace, (int  __c)  );    /* white space sp, \f, \n, \r, \t, \v*/
  36. _PROTOTYPE( int isupper, (int  __c)  );    /* upper-case letter [A-Z] */
  37. _PROTOTYPE( int isxdigit,(int  __c)  );    /* hex digit [0-9], [a-f], [A-F] */
  38.  
  39. /* Macros for identifying character classes. */
  40. #define isalnum(c)    ((__ctype+1)[c]&(_U|_L|_N))
  41. #define isalpha(c)    ((__ctype+1)[c]&(_U|_L))
  42. #define iscntrl(c)    ((__ctype+1)[c]&_C)
  43. #define isdigit(c)    ((__ctype+1)[c]&_N)
  44. #define isgraph(c)    ((__ctype+1)[c]&(_P|_U|_L|_N))
  45. #define islower(c)    ((__ctype+1)[c]&_L)
  46. #define isprint(c)    ((__ctype+1)[c]&(_SP|_P|_U|_L|_N))
  47. #define ispunct(c)    ((__ctype+1)[c]&_P)
  48. #define isspace(c)    ((__ctype+1)[c]&_S)
  49. #define isupper(c)    ((__ctype+1)[c]&_U)
  50. #define isxdigit(c)    ((__ctype+1)[c]&(_N|_X))
  51. #define isascii(c)    ((unsigned) ((c) + 1) < 129)
  52.  
  53. /* The following two macros are weird to keep the Language Police at bay.
  54.  * The macro 'tolower' only affects upper case letters, and 'toupper'
  55.  * only affects lower case letters.  Neither one is permitted to evaluate
  56.  * its argument more than once.  Thus a simple definition like:
  57.  *
  58.  *    #define tolower(c)    (isupper(c) ? c - 'A' + 'a' : c)
  59.  *
  60.  * is prohibited because the argument 'c' is evaluated twice.
  61.  * It might be an expression that has side effects, such as a function
  62.  * call that increments a counter and returns its value as a character.
  63.  * The solution is to first copy the argument to a scratch variable, __tmp.
  64.  */
  65.  
  66. #define tolower(c) (__tmp = (c), isupper(__tmp) ? __tmp - 'A' + 'a' : __tmp)
  67. #define toupper(c) (__tmp = (c), islower(__tmp) ? __tmp - 'a' + 'A' : __tmp)
  68.  
  69. #endif /* _CTYPE_H */
  70.